﻿using HCMS.CommonDtos;
using HCMS.Entities.Manage;
using HCMS.Extensions;
using HCMS.IRepositories.Manage;
using HCMS.IServices.Manage;
using HCMS.Repositories.Manage;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace HCMS.Services.Manage
{
    /// <summary>
    /// 管理员 登录日志 应用服务
    /// </summary>
    public class AdminLoginLogsAppService : CommonAppService<ManageDbContext, AdminLoginLogs>, IAdminLoginLogsAppService
    {
        private readonly IRepository<AdminLoginLogs> baseAdminLoginLogsRepository;
        private readonly IAdminLoginLogsRepository adminLoginLogsRepository;
        private readonly IAdminRepository adminRepository;

        public AdminLoginLogsAppService(
            IRepository<AdminLoginLogs> baseAdminLoginLogsRepository,
            IAdminLoginLogsRepository adminLoginLogsRepository,
            IAdminRepository adminRepository
            ) : base(baseAdminLoginLogsRepository)
        {
            this.baseAdminLoginLogsRepository = baseAdminLoginLogsRepository;
            this.adminLoginLogsRepository = adminLoginLogsRepository;
            this.adminRepository = adminRepository;
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<object>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            if (!string.IsNullOrWhiteSpace(searchArgs.SearchName) && !string.IsNullOrWhiteSpace(searchArgs.SearchKey))
            {
                // 【管理员 姓名】转为【管理员 Id】搜索
                if (searchArgs.SearchName.IsSame("adminName"))
                {
                    searchArgs.SearchName = "ids";
                    var adminIdList = (await adminRepository.GetListAsync()).Where(t => t.AdminName.Contains(searchArgs.SearchKey))?.Select(t => t.Id).ToList();
                    if (adminIdList != null && adminIdList.Any())
                    {
                        searchArgs.SearchKey = string.Join(",", adminIdList);
                    }
                    else
                    {
                        searchArgs.SearchKey = "";
                    }
                }
            }

            var logs = await adminLoginLogsRepository.ManageList(searchArgs, pager);

            var result = new ListPagerDto<object>();
            result.PagerInfo = logs.PagerInfo;

            var data = new List<object>();
            foreach (var item in logs.Data)
            {
                // 关联 管理员 名称
                var adminName = string.Empty;
                var adminInfo = await adminRepository.FirstOrDefaultAsync(t => t.Id == item.AdminId);
                if (adminInfo != null)
                {
                    adminName = adminInfo.AdminName;
                }

                data.Add(new
                {
                    id = item.Id,
                    createTime = item.CreateTime,
                    adminId = item.AdminId,
                    adminName = adminName,
                    isSuccess = item.IsSuccess,
                    message = item.Message,
                    refererUrl = item.RefererUrl,
                    url = item.Url,
                    ip = item.Ip,
                    brower = item.Brower
                });
            }

            result.Data = data;

            return result;
        }
    }
}
